package com.xyf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.domain.UserAddress;
import com.xyf.sys.service.IUserAddressService;
import com.xyf.sys.util.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author R-H
 * @Date 2024/2/241533
 * @Description
 */
@RestController
@RequestMapping("/userAddress")
@Slf4j
@Api(tags = "用户收寄件地址相关")
public class UserAddressController {

    @Autowired
    private IUserAddressService userAddressService;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;

    @ApiOperation("收寄件地址列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true),
            @ApiImplicitParam(name = "type", value = "类型 1 寄件 2 收件", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = true),
    })
    @PostMapping("/list")
    public Object getList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String appId = request.getHeader("APPID") == null ? "" : request.getHeader("APPID");
        String userId = appId.equals(clientConfig.getAppid()) ? map.get("userId").toString() : null;;
        Integer type = (Integer) map.get("type");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String keyword = map.get("keyword").toString();
        Long deptId = appId.equals(clientConfig.getAppid()) ? null : Long.valueOf(map.get("deptId").toString());
        PageInfo page = userAddressService.getPage(pageNum, pageSize, userId, type, keyword, deptId, clientConfig.getAppid().equals(appId));
        return R.ok(page);
    }


    @ApiOperation("新增收寄件地址")
    @PostMapping("/add")
    public Object add(@RequestBody UserAddress userAddress, HttpServletRequest request) {
        String appId = request.getHeader("APPID") == null ? "" : request.getHeader("APPID");
        appId.equals(clientConfig.getAppid());
        return R.ok(userAddressService.add(userAddress));
    }

    @ApiOperation("编辑收寄件地址")
    @PostMapping("/edit")
    public Object edit(@RequestBody UserAddress userAddress) {
        return R.ok(userAddressService.update(userAddress));
    }

    @ApiOperation("删除一个收寄件地址")
    @PostMapping("/remove/{id}")
    public Object remove(@PathVariable @ApiParam(value = "id") Long id) {
        return R.ok(userAddressService.remove(id));
    }
}
